package xju_9_10.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xju_9_10.entity.User;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {


    //    @RequestMapping(value = "/test",method = RequestMethod.GET)
    @GetMapping("/test")
    @ResponseBody
    public String test() {
        System.out.println("test");
        return "test";
    }

    @RequestMapping("/test1")
    public String test1() {
        System.out.println("test1");
        return "hello";
    }

    @GetMapping("/data")
    @ResponseBody
    public String test2(@RequestParam("name") String username, @RequestParam(value = "password", required = false, defaultValue = "111") String password) {
        System.out.println(username);
        System.out.println(password);
        return username + ":" + password;
    }

    @RequestMapping("/data1")
//    @ResponseBody
    public String test2(@RequestBody User user) {
        System.out.println(user);
        return "hello";
    }

    @RequestMapping("/data2")
//    @ResponseBody
    public String test2(@RequestParam("num") List<String> num) {
        System.out.println(num);
        return "hello";
    }

    @RequestMapping("/delete/{id}/{name}")
//    @ResponseBody
    public String test2(@PathVariable("id") Integer id,@PathVariable("name") String name) {
        System.out.println(id);
        System.out.println(name);
        return "hello";
    }

    // cookie
    @RequestMapping("/cookie")
    public String test3(@CookieValue("JSESSIONID") String cookie) {
        System.out.println(cookie);
        return "hello";
    }

    // requestheader
    @RequestMapping("requestHeader")
    public String test4(@RequestHeader("host")String host,@RequestHeader("accept" )String accept,@RequestHeader(value = "test",required = false)String test ) {
        System.out.println(host);
        System.out.println(accept);
        System.out.println(test);
        return "hello";
    }

    // request 级别共享域
    @RequestMapping("/request")
    public String test5(Model model, ModelMap modelMap, Map<String, String> map, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("model", "modelData");
        modelMap.addAttribute("modelMap", "modelMapData");
        map.put("map", "mapData");
        request.setAttribute("request", "requestData");
        return "hello";
    }

    @RequestMapping("/modelAndView")
    public ModelAndView test6(ModelAndView modelAndView) {
        modelAndView.addObject("modelAndView", "modelAndViewData");
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    // session
    @RequestMapping("/session")
    public String test7(HttpSession session) {
        session.setAttribute("session", "sessionData");
        return "hello";
    }

    // servletContext application
    @Autowired
    private ServletContext servletContext;
    @RequestMapping("/applicaton")
    public String test8() {
        servletContext.setAttribute("servletContext", "servletContextData");
        return "hello";
    }

    // 转发
    @RequestMapping("/forward")
    public String test9() {
        return "forward:/user/request";
    }

    // 重定向
    @RequestMapping("/redirect")
    public String test10() {
        return "redirect:/user/modelAndView";
    }




}
